package com.yjt.admin.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.admin.system.dao.OrgSuperLeaderMapper;
import com.yjt.admin.system.entity.OrgSuperLeader;
import com.yjt.admin.system.service.OrgSuperLeaderService;
import com.yjt.admin.system.vo.org.OrgLeaderVO;
import com.yjt.admin.system.vo.org.OrgVO;
import com.yjt.common.utils.ComUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 组织上级领导关系表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2021-10-14
 */
@Service
public class OrgSuperLeaderServiceImpl extends ServiceImpl<OrgSuperLeaderMapper, OrgSuperLeader> implements OrgSuperLeaderService {

    @Resource
    private OrgSuperLeaderMapper orgSuperLeaderMapper;

    @Override
    public void addOrgSuperLeader(List<String> orgSuperLeaderIdList, String orgId) {
        //增加公司上级领导/部门上级领导 --可选择人员，岗位并且可多选
        if (ComUtil.isNotNullList(orgSuperLeaderIdList)) {
            List<OrgSuperLeader> orgSuperLeaderPoList = orgSuperLeaderIdList.stream().map(orgSuperLeaderId -> {
                return new OrgSuperLeader(null, orgSuperLeaderId, orgId);
            }).collect(Collectors.toList());
            this.saveBatch(orgSuperLeaderPoList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrgSuperLeader(List<String> orgSuperLeaderIdList, String orgId) {

        //增加公司上级领导/部门上级领导 --可选择人员，岗位并且可多选
        if (ComUtil.isNotNullList(orgSuperLeaderIdList)) {

            //先删除上级关系
            this.remove(Wrappers.<OrgSuperLeader>lambdaUpdate().eq(OrgSuperLeader::getOrgId, orgId));

            //保存数据
            List<OrgSuperLeader> orgSuperLeaderPoList = orgSuperLeaderIdList.stream().map(orgSuperLeaderId -> {
                return new OrgSuperLeader(null, orgSuperLeaderId, orgId);
            }).collect(Collectors.toList());
            this.saveBatch(orgSuperLeaderPoList);
        }
    }

    @Override
    public void setOrgSuperLeader(OrgVO orgVO) {
        //查询公司上级领导/部门上级领导
        if (ComUtil.isNotNullStr(orgVO.getOrgSuperLeaderIds())) {
            List<String> orgSuperLeaderIdList = Arrays.asList(orgVO.getOrgSuperLeaderIds().split(","));
            List<OrgLeaderVO> orgSuperLeaderVOList = orgSuperLeaderMapper.findOrgSuperLeaderById(orgSuperLeaderIdList);
            List<String> orgSuperLeaderNameList = new ArrayList<>();
            orgSuperLeaderIdList.stream().forEach(orgSuperLeaderId -> {
                OrgLeaderVO orgLeaderVO = orgSuperLeaderVOList.stream().filter(
                        OrgLeaderVO -> StringUtils.equals(orgSuperLeaderId, OrgLeaderVO.getEmplyOrPostId())).findFirst().get();
                orgSuperLeaderNameList.add(orgLeaderVO.getEmpOrPostName());
            });
            orgVO.setOrgLeaderIdList(orgSuperLeaderIdList);
            orgVO.setOrgLeaderNameList(orgSuperLeaderNameList);
        }
    }
}
